package com.zz.framework.core.entity

import com.zz.apk.utils.RealTimeUtil
import com.zz.framework.core.BaseApp
import com.zz.framework.core.event.AccountBannedEvent
import com.zz.framework.core.event.TokenFiredEvent
import com.zz.framework.core.ext.*
import com.zz.framework.R
import org.greenrobot.eventbus.EventBus
import java.util.*

/**
 * Copyright © 2019 zanezhao All rights reserved.
 * Created by zanezhao on 2019-11-13 15:40.
 *
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
data class HttpResponse<out T>(
    val code: Int = 0
    , val success: Boolean = false
    , val message: String? = null
    , val total: Int = 0
    , val count: Int = 0
    , val countImg:Int?
    , val countVideo:Int?
    , val stime: Long = 0
    , val data: T? = null
) {

    fun isSuccess(): Boolean {
        return this.code == 200
    }

    /**
     *  全局默认实现, 可根据自身业务 重写execute方法
     *  @param error            若有错误的回调, 默认status
     *  @param successResponse  成功的回调, 默认是返回 HttpResponse<T>
     */
    fun executeRsp(
        successResponse: ((HttpResponse<T>) -> Unit)?,
        error: ((HttpStatus) -> Unit)? = null
    ) {
        RealTimeUtil.updateServerTime(this.stime)
        if (isSuccess()) {
            successResponse?.invoke(this)
        }else {
            handleError(error)
        }
    }

    private fun handleError(error: ((HttpStatus) -> Unit)?) {
        if (this.code == 10010) {
            //token失效
            EventBus.getDefault().post(TokenFiredEvent())
        } else if (this.code == 10022) {
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.YEAR, 100)
            val time = this.message.getLong().times(1000)
            if (time > calendar.timeInMillis) {
                //永久被封号
                EventBus.getDefault().post(AccountBannedEvent(getString(R.string.text_account_banned_permanently)))
            } else {
                //被封号
                EventBus.getDefault().post(AccountBannedEvent(BaseApp.context.getString(R.string.text_account_banned_,time.toDateString(timeFormatStringYMDHM3))))
            }
        }
        else if (code == 10020){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_user_not_exist) , code = code))
        }
        else if (code == 10021){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_user_not_exist), code = code))
        }
        else if (code == 20002){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_prohibited_comments_,this.message.getLong().times(1000).toDateString(timeFormatStringYMDHM3)), code = code))
        }
        else if (code == 20003){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_prohibit_publish_,this.message.getLong().times(1000).toDateString(timeFormatStringYMDHM3)), code = code))
        }
        else if (code == 20004){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_prohibit_send_msg_,this.message.getLong().times(1000).toDateString(timeFormatStringYMDHM3)), code = code))
        }
        else if (code == 20005){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_prohibit_share_,this.message.getLong().times(1000).toDateString(timeFormatStringYMDHM3)), code = code))
        }
        else if (code == 20006){
            error?.invoke(HttpStatus(message = BaseApp.context.getString(R.string.text_state_error_tip_,this.message.getLong().times(1000).toDateString(timeFormatStringYMDHM3)), code = code))
        }
        else {
            error?.invoke(HttpStatus(message = message ?: "", code = code))
        }
    }


    /**
     *  全局默认实现, 可根据自身业务 重写execute方法
     *  @param success          成功的回调, 默认是返回 data
     *  @param error            若有错误的回调, 默认status
     */
    fun execute(success: ((T?) -> Unit)?, error: ((HttpStatus) -> Unit)? = null) {
        RealTimeUtil.updateServerTime(this.stime)
        if (isSuccess()) {
            success?.invoke(this.data)
        } else{
            handleError(error)
        }
    }



//    fun executeList(success: ((List<T>?) -> Unit)?, error: ((HttpStatus) -> Unit)? = null) {
//        GlobalSetting.setStime(this.stime)
//        if (isSuccess()) {
//            success?.invoke(this.objList)
//        } else {
//            error?.invoke(HttpStatus(message = message ?: ""))
//        }
//    }
}

data class HttpStatus(val code: Int = 0, val message: String)